package org.crud4j.core;

import java.util.Map;

/**
 * Crud service. Interface that works only with crud metadata and maps the given
 * values to the underlying persistent strategy.
 */
public interface CrudService {

	/**
	 * Recommended JNDI address where the crud service should be deployed
	 */
	public static String JNDI = "crud/service";

	/**
	 * List of supported beans
	 * 
	 * @return the list of supported beans
	 */
	public Class[] listBeanClasses();

	/**
	 * Creates a new empty CrudBean
	 * 
	 * @param type
	 *            the crud bean class
	 * @return a new empty CrudBean for the given class
	 */
	public CrudBean create(Class type);

	/**
	 * Finds the bean with the given ids
	 * 
	 * @param type
	 *            the bean class
	 * @param ids
	 *            the bean ids
	 * @return the bean that matches the given ids
	 */
	public CrudBean find(Class type, Map<String, String> ids);

	/**
	 * Browses all entities of the given type.
	 * 
	 * @param type
	 *            the bean class
	 * @return all beans for the given class in a wrapper
	 */
	public CrudBrowsePage findAll(Class type);

	/**
	 * Persists the given bean
	 * 
	 * @param bean
	 *            the crud bean
	 * @return the persisted bean
	 * @throws HasRemoveMarkException
	 *             if the bean is already marked as removed
	 * @throws IdInUseException
	 *             if the given crudbean is new and the id is already in use by
	 *             another crud bean
	 */
	public CrudBean persist(CrudBean crudBean) throws RemoveMarkException,
			IdInUseException;

	/**
	 * Removes the bean
	 * 
	 * @param bean
	 */
	public void remove(CrudBean crudBean);

	/**
	 * Browse a sublist of the beans
	 * 
	 * @param type
	 *            the bean class
	 * @param pageSize
	 *            the page size
	 * @param page
	 *            the browse page, the offset
	 * @return a browsing page with a limited collection of beans
	 */
	public CrudBrowsePage browse(Class type, Integer pageSize, Integer page);

	/**
	 * Browses the last page of the bean collection
	 * 
	 * @param type
	 *            the bean class
	 * @param pageSize
	 *            the page size
	 * @return a browsing page a limited collection of beans
	 */
	public CrudBrowsePage browseLast(Class type, Integer pageSize);

	/**
	 * Filtered browse
	 * 
	 * @param type
	 *            the bean class
	 * @param pageSize
	 *            the page size
	 * @param page
	 *            the offset, or page number
	 * @param filters
	 *            a collection of filters
	 * @return a browsing page with all the beans that matches the given
	 *         condition
	 */
	public CrudBrowsePage filteredBrowse(Class type, Integer pageSize,
			Integer page, Map<String, String> filters);

	/**
	 * Browse the last page of the beans
	 * 
	 * @param type
	 *            the bean class
	 * @param pageSize
	 *            the page size
	 * @param filters
	 *            a collection of filters
	 * @return the last browsing page with the beans that matches the given
	 *         condition
	 */
	public CrudBrowsePage filteredBrowseLast(Class type, Integer pageSize,
			Map<String, String> filters);

}
